<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<div class="layui-form layuimini-form" lay-filter="room-date-filter">
    <div class="layui-form-item">
        <label class="layui-form-label required">标题</label>
        <div class="layui-input-block">
            <input type="text" name="noticeTitle" lay-verify="required" lay-reqtext="标题不能为空" placeholder="请输入标题" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">内容</label>
        <div class="layui-input-block">
            <input type="text" name="noticeContent" lay-verify="required" lay-reqtext="内容不能为空" placeholder="请输入内容" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">开始日期</label>
        <div class="layui-input-block">
            <input type="text" name="date" id="date" lay-verify="date" placeholder="yyyy-MM-dd" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">结束日期</label>
        <div class="layui-input-block">
            <input type="text" name="enddate" id="enddate" lay-verify="date" placeholder="yyyy-MM-dd" autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
        </div>
    </div>
</div>
</div>
<script src="../../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'laydate'], function () {
        var form = layui.form,
            layer = layui.layer,
            $ = layui.$,
            laydate = layui.laydate;

        //获取从父页面传过来的值
        let noticeDate = layui.sessionData("noticeDate");

        function getCaption(obj,state) {
            var index=obj.lastIndexOf("~");
            if (state==0){
                obj=obj.substring(0,index);
            }else {
                obj=obj.substring(index+1,obj.length);
            }
            return obj;
        }

        form.val("room-date-filter",{
            "noticeTitle":noticeDate.notice.ofNoticeTitle
            ,"noticeContent":noticeDate.notice.ofNoticeContent
            ,"date":getCaption(noticeDate.notice.ofNoticeTime,0)
            ,"enddate":getCaption(noticeDate.notice.ofNoticeTime,1)
        });

        //日期
        laydate.render({
            elem: '#date'
        });
        laydate.render({
            elem: '#enddate'
        });

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            var result=JSON.stringify(data.field);
            result = JSON.parse(result);
            var msg = {
                ofNoticeId:noticeDate.notice.ofNoticeId,
                ofNoticeTitle:result.noticeTitle,
                ofNoticeContent:result.noticeContent,
                ofNoticeTime:result.date+'~'+result.enddate
            };
            console.log(msg);
            layer.confirm('确定要修改吗？',function (index) {
                layer.close(index);
                $.ajax({
                    url: '../../../../notice/saveNotice',
                    type: 'post',
                    contentType: 'application/json',
                    data: JSON.stringify(msg),
                    dataType: 'json',
                    success:function (data) {
                        if (data.code == '200'){
                            var index = layer.alert("修改成功！", function () {
                                // 关闭弹出层
                                layer.close(index);

                                var iframeIndex = parent.layer.getFrameIndex(window.name);
                                //关闭当前frame
                                parent.layer.close(iframeIndex);
                                //修改成功后刷新父界面
                                window.parent.location.reload();
                            });
                        }else {
                            layer.msg("修改失败！");
                        }
                    },
                    error:function (error) {
                        layer.msg("修改失败！");
                    }
                });
            });

            return false;
        });

    });
</script>
</body>
</html>